package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Supplier;
import com.ericom.service.GenericManager;

public class SupplierAction extends BaseAction {
	private GenericManager<Supplier, Long> supplierManager;
	private List suppliers;
	private Supplier supplier;
	private Long id;

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSupplierManager(
			GenericManager<Supplier, Long> supplierManager) {
		this.supplierManager = supplierManager;
	}

	public String list() {
		suppliers = supplierManager.getAll();
		return SUCCESS;
	}

	public List getSuppliers() {
		return suppliers;
	}

	public String delete() {
		supplierManager.remove(supplier.getId());
		this.saveMessage(getText("supplier.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			supplier = supplierManager.get(id);
		} else {
			supplier = new Supplier();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (supplier.getId() == null);
		supplier = supplierManager.save(supplier);

		String key = (isNew) ? "supplier.added" : "supplier.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
